//package com.speedchina.oauth2_permission.config;
//
//import org.springframework.beans.factory.annotation.Value;
//import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
//import org.springframework.context.annotation.Bean;
//import org.springframework.context.annotation.Configuration;
//import springfox.documentation.builders.ApiInfoBuilder;
//import springfox.documentation.builders.OAuthBuilder;
//import springfox.documentation.builders.PathSelectors;
//import springfox.documentation.builders.RequestHandlerSelectors;
//import springfox.documentation.service.*;
//import springfox.documentation.spi.DocumentationType;
//import springfox.documentation.spi.service.contexts.SecurityContext;
//import springfox.documentation.spring.web.plugins.Docket;
//import springfox.documentation.swagger.web.SecurityConfiguration;
//import springfox.documentation.swagger.web.SecurityConfigurationBuilder;
//import springfox.documentation.swagger2.annotations.EnableSwagger2;
//
//import java.util.ArrayList;
//import java.util.Arrays;
//import java.util.Collections;
//import java.util.List;
//
//@Configuration
//@EnableSwagger2
//@ConditionalOnProperty(prefix = "swagger2", name = "auth.clientId")
//public class Swagger2Config {
//
//    private static final String VERSION = "1.0";
//    private String title = "系统接口文档";
//    private String description = "提供系统接口文档";
//    private String basePackage = "com.bestaone.hiauth";
//
//    @Value("${swagger2.auth.clientId:}")
//    private String clientId;
//    @Value("${swagger2.auth.clientSecret:}")
//    private String clientSecret;
//    @Value("${swagger2.auth.authorizationUri:}")
//    private String authorizationUri;
//    @Value("${swagger2.auth.tokenUri:}")
//    private String tokenUri;
//    @Value("${swagger2.auth.scopes:}")
//    private String scopes;
//
//    private ApiInfo apiInfo() {
//        return new ApiInfoBuilder()
//                .title(title)
//                .description(description)
//                .version(VERSION).build();
//    }
//
//    @Bean
//    public Docket apiDocket() {
//        return new Docket(DocumentationType.SWAGGER_2).select()
//                .apis(RequestHandlerSelectors.basePackage(basePackage))
//                .paths(PathSelectors.any()).build()
//                .apiInfo(apiInfo())
//                .securitySchemes(Collections.singletonList(securityScheme()))
//                .securityContexts(Collections.singletonList(securityContext()));
//    }
//
//    private SecurityScheme securityScheme() {
//        return new OAuthBuilder()
//                .name("OAuth2")
//                .grantTypes(grantTypes())
//                .scopes(Arrays.asList(scopes()))
//                .build();
//    }
//
//    private SecurityContext securityContext() {
//        return SecurityContext.builder()
//                .securityReferences(Collections.singletonList(new SecurityReference("OAuth2", scopes())))
//                .forPaths(PathSelectors.any())
//                .build();
//    }
//
//    private AuthorizationScope[] scopes() {
//        String[] list = scopes.trim().split(",");
//        AuthorizationScope[] authorizationScopes = new AuthorizationScope[list.length];
//        for(int i=0; i<list.length; i++){
//            ResourceDomainType resourceDomainType = ResourceDomainType.valueOf(list[i].trim());
//            authorizationScopes[i] = new AuthorizationScope(resourceDomainType.name(), resourceDomainType.getText());
//        }
//        return authorizationScopes;
//    }
//
//    @Bean
//    List<GrantType> grantTypes() {
//        List<GrantType> grantTypes = new ArrayList<>();
//        TokenRequestEndpoint tokenRequestEndpoint = new TokenRequestEndpoint(authorizationUri, clientId, clientSecret);
//        TokenEndpoint tokenEndpoint = new TokenEndpoint(tokenUri, "access_token");
//        grantTypes.add(new AuthorizationCodeGrant(tokenRequestEndpoint, tokenEndpoint));
//        return grantTypes;
//    }
//
//    @Bean
//    public SecurityConfiguration securityInfo() {
//        return SecurityConfigurationBuilder.builder()
//                .clientId(clientId)
//                .clientSecret(clientSecret)
//                .realm("realm")
//                .appName("HiAuth API DOC")
//                .scopeSeparator(",")
//                .additionalQueryStringParams(null)
//                .useBasicAuthenticationWithAccessCodeGrant(false)
//                .build();
//    }
//
//}